﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using WMSABL;
using SYDZ.WMS.DBUtility;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.T_WMS.Basic
{
    public partial class MachineFrm : Form
    {
        private string strWhere1 = "";
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        public MachineFrm()
        {
            InitializeComponent();
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 50;
            this.pageControl1.Bind();
        }

        #region 数据
        private int DBindData1()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_ProductModel";
            pageData.PrimaryKey = "Models_Id";
            pageData.OrderStr = "Models_Id";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere1;
            pageData.QueryFieldName = "*";
            pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = this.pageControl1.bindingSource;
            dataGridView1.ClearSelection();
            dataGridView1.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl1_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData1();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            strWhere1 = " 1=1 ";
            if (!SystemVariable.VaildationPermission("查询")) return;
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                strWhere1 += " and Models_NO='" + textBox1.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                strWhere1 += " and Models_Name='" + textBox2.Text.Trim() + "'";
            }
            pageControl1.Bind();
        }

        private void btnNewUser_Click(object sender, EventArgs e)
        {
            this.btnUSave.Text = "添 加";
            this.tabAnalysisList.SelectedIndex = 1;
        }
        WMSAML.Warehouse.Model.T_ProductModel tpmodel = new WMSAML.Warehouse.Model.T_ProductModel();
        WMSAML.Warehouse.BLL.T_ProductModel tpBll = new WMSAML.Warehouse.BLL.T_ProductModel();
        private void btnUSave_Click(object sender, EventArgs e)
        {
            string str1 = txt1.Text.Trim();
            string str2 = txt2.Text.Trim();
            tpmodel.Models_Name = str1;
            tpmodel.Models_Fake = str1;
            tpmodel.Models_NO = str2;
            string sql = " select count(1) from T_ProductModel where Models_NO='" + str1 + "' AND Models_Name='" + str2 + "' ";
            DataSet dt1 = new DataSet();
            dt1 = bll_PublicDataSet.GetDataSet(sql);
            if (dt1 != null)
            {
                if (dt1.Tables[0].Rows[0][0].ToString() != "0")
                {
                    MessageClass.warning("此机型已存在！");
                }
                else
                {
                    if (btnUSave.Text == "添 加")
                    {
                        if (MessageClass.enquire("确认新增？") == DialogResult.OK)
                        {
                            if (tpBll.Add(tpmodel) > 0)
                            {
                                _NID = 0;
                                txt1.Text = "";
                                txt2.Text = "";
                                SystemVariable.AddLogInfo("机型维护", "新增机型信息。");
                                MessageClass.right("操作成功！");
                            }
                            else
                            {
                                MessageClass.error("操作失败！");
                            }
                        }
                    }
                    else
                    {
                        if (MessageClass.enquire("确认编辑？") == DialogResult.OK)
                        {
                            int result = DbHelperSQL.ExecuteSql("update T_ProductModel set Models_NO='" + str1 + "',Models_Fake='" + str1 + "',Models_Name='" + str2 + "' where Models_Id='" + _NID + "' ");
                            if (result > 0)
                            {
                                _NID = 0;
                                txt1.Text = "";
                                txt2.Text = "";
                                pageControl1.Bind();
                                SystemVariable.AddLogInfo("机型维护", "编辑机型信息。");
                                MessageClass.right("操作完成！");
                                btnUSave.Text = "添 加";
                                tabAnalysisList.SelectedIndex = 0;
                            }
                            else
                            {
                                MessageClass.error("操作失败！");
                            }
                        }
                    }
                }
            }
        }
        private int _NID = 0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _NID = 0;
            if (e.RowIndex > -1)
            {
                if (this.dataGridView1.Rows.Count != 0)
                {
                    #region 获取单列详细信息
                    if (e.ColumnIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 0)
                    {
                        _NID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Models_Id"].Value.ToString());
                        txt1.Text = dataGridView1.CurrentRow.Cells["Models_NO"].Value.ToString();
                        txt2.Text = dataGridView1.CurrentRow.Cells["Models_Name"].Value.ToString();
                        btnUSave.Text = "修 改";
                        tabAnalysisList.SelectedIndex = 1;
                    }
                    else
                    {
                        _NID = 0;
                        txt1.Text = "";
                        txt2.Text = "";
                    }
                    #endregion

                    if (e.ColumnIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 1)
                    {
                        _NID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Models_Id"].Value.ToString());
                        if (_NID == 0)
                        {
                            MessageClass.error("请选择信息！"); return;
                        }
                        if (MessageClass.enquire("删除选择?") == DialogResult.OK)
                        {
                            string SelectSQL = " delete from T_ProductModel where Models_Id='" + _NID + "' ";
                            int result = DbHelperSQL.ExecuteSql(SelectSQL);
                            if (result > 0)
                            {
                                _NID = 0;
                                pageControl1.Bind();
                                SystemVariable.AddLogInfo("机型维护", "删除机型信息。");
                                MessageClass.right("操作完成！");
                            }
                            else
                            {
                                MessageClass.error("操作失败！");
                            }
                        }
                    }
                }
            }
        }

        #region  
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }
        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }
        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        private void btnRetunList_Click(object sender, EventArgs e)
        {
            tabAnalysisList.SelectedIndex = 0;
        }

        private void btnUClear_Click(object sender, EventArgs e)
        {
            txt1.Text = "";
            txt2.Text = "";
            _NID = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MachineFPFrm frm = new MachineFPFrm();
            frm.Show();
        }
        #endregion
    }
}
